#!/usr/bin/perl
use strict;

my($CNT_SEQ, $PROB_ERR) = (200, 20);
open (TEST, ">test.fa") or die;
open (IDS, ">test1.ids") or die;
use constant ERR_NOSEQ => 1;
use constant ERR_NOACC => 2;
use constant ERR_DUPLI => 3;
use constant ERR_NO_ID => 4;
my (
$cnt_err_noseq,
$cnt_err_noacc,
$cnt_err_dupli,
$cnt_err_no_id) = (1,0,-1,0);

my $cnt_seq = 0;
{
    print TEST "ASDFASDFASFAS\n";
    for (0..($CNT_SEQ - 1))
    {
    	++$cnt_seq;
		my $error = int(rand($PROB_ERR));
		my $id = '';
		if ($error == ERR_NO_ID)
		{
			$id = "seq_#$cnt_seq";
			++$cnt_err_no_id;
		}
		elsif ($error == ERR_DUPLI)
		{
			$id = "sequence_#99999";
			++$cnt_err_dupli;
		}
		else
		{
			$id = "sequence_#$cnt_seq";
		}
		print TEST ">$id ASDFASDF\n";
    	if ($error == ERR_NOSEQ)
		{
			++$cnt_err_noseq;
			redo;
		}
    	for (0..15)
    	{
    		print TEST "line $_ ASDFSAFASF\n";
    	}
    	print TEST "last line ASDFSAFASF\n";
		next if ($error == ERR_DUPLI && $cnt_err_dupli);
		next if ($error == ERR_NO_ID);
		print IDS  "$id\n";
    }
}
print $cnt_err_noseq, "\tmissing sequences\n";
print $cnt_err_no_id, "\tmissing identifiers\n";
print $cnt_err_dupli, "\tduplicated identifiers\n";


system ('fasta_list_identifiers -r "sequence_#\d+" < test.fa -e error.ids -d -v > test2.ids');
print STDERR "\tComparing results...\n";
{
	my @diff = `diff test2.ids test1.ids`;
	if (@diff != 0)
	{
		print STDERR "FAILED: test2.ids test1.ids are not the same!\n";
		exit;
	}
}
print STDERR "\tCleaning up...\n";
unlink (glob("*.fa"));
unlink (glob("*.ids"));
print STDERR "\n\tSUCCESS!!!\n\n";
